/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Core;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author Mr Bao
 */
public class Dispatcher implements Runnable {

    protected List queue;
    protected boolean running;
    private static final long delay = 5l;

    public Dispatcher() {
        queue = Collections.synchronizedList(new LinkedList());
    }

    public void Queue(Message message) {
        queue.add(message);
    }

    public void Flush() {
        while (queue.size() > 0) {
            Message message = (Message) queue.remove(0);
            message.Send();
        }
    }

    public void run() {
        while (running) {
            try {
                Flush();
                Thread.sleep(delay);
            } catch (Exception e) {
            }
        }
    }

    public void Halt() {
        running = false;
    }
}
